<?php
namespace app\admin\model;

use think\Db;
use think\Model;

class UserModel extends Model
{

    protected $table='lt_users';
    public function getusersList($field ="*",$limit=15,$where=[],$searchKeyWords=''){
        foreach ($where as $k=>$v){
                if(!$v)unset($where[$k]);
        }
        return  Db::name('users')
            ->where($where)
            ->field($field)
            ->whereLike("nickname|telephone|share_code","%{$searchKeyWords}%")
            ->order('id','desc')
            ->paginate($limit,[]);
    }
    public static function getAgentList($field ="*",$limit=10){
        return  Db::name('users_agent')->cache(true)->order('price','asc')->paginate($limit,[]);
    }
    public static function getAgentInfoByid($id,$field="*"){
        return  Db::name('users_agent')
            ->where('id',$id)
            ->field($field)
            ->find();
    }
    public static function getUserIdByTelephone($telephone,$field="*"){
        return self::where('telephone',$telephone)->field($field)->find();
    }
    public static function getUserInfo($uerId){
        return self::join('users_idcard card','card.user_id = user.id','LEFT')
            ->alias('user')
            ->field('
            user.id as user_id,
            user.nickname,
            user.email,
            user.telephone,
            user.sex,
            user.telephone,
            user.lock,
            user.agent_name,
            user.true_name,
            user.create_time,
            user.old_telephone,
            user.parent_telephone,
            user.alipay,
            user.operate_name,
            user.share_code,
            user.parent_agent_telephone,
            user.user_job_name,
            user.lt_number,
            user.commission,
            user.recommend_number,
            user.login_time,
            user.login_ip,
            card.front,
            card.back,
            card.IDCard
        
            ')
            ->get($uerId);
    }
    public static function setUserReal($user_id,$true_name){
        return self::where('id',$user_id)
            ->update([
                "is_real"=>1,
                "true_name"=>$true_name,
            ]);
    }
    public static function plusMoney($userId,$money){
        return self::where('id',$userId)->setInc('commission',$money);
    }
    public static function getRegIdByUser($user_id){
        return self::where('id',$user_id)->value('regid');
    }
    public static function getRegAllByUser($user_in){
        return self::wherein('id',$user_in)->field('regid,id as user_id')->select();
    }

    public static function getJobCommissionByUserId($user_id,$field='job_commission,parent_id,id as user_id,user_job_id,true_name,user_job_name,telephone')
    {
        return self::where(['id'=>$user_id])
            ->field($field)
            ->find();
    }
}